<template>
  <div class="main user-layout-register">
    <h3>注册</h3>
    <a-form
      ref="formRegister"
      :autoFormCreate="
        (form) => {
          this.form = form
        }
      "
      id="formRegister"
    >
      <a-form-item fieldDecoratorId="username">
        <a-input size="large" type="text" placeholder="学号（用户名）"></a-input>
      </a-form-item>
      <a-form-item fieldDecoratorId="realname">
        <a-input size="large" type="text" placeholder="姓名"></a-input>
      </a-form-item>
      <a-form-item fieldDecoratorId="major">
        <a-input size="large" type="text" placeholder="专业"></a-input>
      </a-form-item>
      <a-form-item fieldDecoratorId="className">
        <a-input size="large" type="text" placeholder="班级"></a-input>
      </a-form-item>
      <a-form-item fieldDecoratorId="grade">
        <a-input size="large" type="text" placeholder="年级"></a-input>
      </a-form-item>
      <a-form-item fieldDecoratorId="age">
        <a-input size="large" type="text" placeholder="年龄"></a-input>
      </a-form-item>
      <a-form-item fieldDecoratorId="sex">
        <a-input size="large" type="text" placeholder="性别"></a-input>
      </a-form-item>
      <a-form-item fieldDecoratorId="password">
        <a-input size="large" type="password" placeholder="密码"></a-input>
      </a-form-item>
      <!-- <a-popover placement="rightTop" trigger="click" :visible="state.passwordLevelChecked">
        <template slot="content">
          <div :style="{ width: '240px' }">
            <div :class="['user-register', passwordLevelClass]">
              强度：<span>{{ passwordLevelName }}</span>
            </div>
            <a-progress :percent="state.percent" :showInfo="false" :strokeColor="passwordLevelColor" />
            <div style="margin-top: 10px">
              <span>请至少输入 8 个字符。请不要使用容易被猜到的密码。</span>
            </div>
          </div>
        </template>
        <a-form-item
          fieldDecoratorId="password"
          :fieldDecoratorOptions="{ rules: [{ required: false }, { validator: this.handlePasswordLevel }] }"
        >
          <a-input size="large" type="password" autocomplete="false"></a-input>
        </a-form-item>
      </a-popover> -->
      <a-form-item>
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="register-button"
          :loading="registerBtn"
          @click.stop.prevent="handleSubmit"
          :disabled="registerBtn"
          >注册
        </a-button>
        <router-link class="login" :to="{ name: 'login' }">使用已有账户登录</router-link>
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
import { mixinDevice } from '@/utils/mixin.js'
import { getSmsCaptcha } from '@/api/login'
import { getAction, postAction } from '@/api/manage'
import { checkOnlyUser } from '@/api/api'

const levelNames = {
  0: '低',
  1: '低',
  2: '中',
  3: '强',
}
const levelClass = {
  0: 'error',
  1: 'error',
  2: 'warning',
  3: 'success',
}
const levelColor = {
  0: '#ff0000',
  1: '#ff0000',
  2: '#ff7e05',
  3: '#52c41a',
}
export default {
  name: 'Register',
  components: {},
  mixins: [mixinDevice],
  data() {
    return {
      form: null,

      state: {
        time: 60,
        smsSendBtn: false,
        passwordLevel: 0,
        passwordLevelChecked: false,
        percent: 10,
        progressColor: '#FF0000',
      },
      registerBtn: false,
    }
  },
  computed: {
    // passwordLevelClass() {
    //   return levelClass[this.state.passwordLevel]
    // },
    // passwordLevelName() {
    //   return levelNames[this.state.passwordLevel]
    // },
    // passwordLevelColor() {
    //   return levelColor[this.state.passwordLevel]
    // },
  },
  methods: {
    checkUsername(rule, value, callback) {
      if (!value) {
        callback(new Error('请输入用户名'))
      } else {
        var params = {
          username: value,
        }
        checkOnlyUser(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback('用户名已存在!')
          }
        })
      }
    },
    handleSubmit() {
      console.log('11111111111')
      this.form.validateFields((err, values) => {
        console.log(values, 'values')
        if (!err) {
          console.log(values, '的v花花viu啊')
          if (!values.username) {
            this.$message.warning('请输入用户名')
            return
          } else if (!values.password) {
            this.$message.warning('请输入密码')
            return
          } else if (!values.realname) {
            this.$message.warning('请输入姓名')
            return
          } else if (!values.major) {
            this.$message.warning('请输入专业')
            return
          } else if (!values.className) {
            this.$message.warning('请输入班级')
            return
          } else if (!values.grade) {
            this.$message.warning('请输入年级')
            return
          } else if (!values.age) {
            this.$message.warning('请输入年龄')
            return
          } else if (!values.sex) {
            this.$message.warning('请输入性别')
            return
          }
          var register = {
            username: values.username,
            password: values.password,
            realname: values.realname,
            major: values.major,
            className: values.className,
            grade: values.grade,
            age: values.age,
            sex: values.sex,
          }
          postAction('sys/user/register', register).then((res) => {
            console.log('555555555')
            if (!res.success) {
              console.log('2222222')
              this.registerFailed(res.message)
            } else {
              this.$router.push({ name: 'registerResult', params: { ...values } })
            }
          })
        }
      })
    },
    registerFailed(message) {
      this.$notification['error']({
        message: '注册失败',
        description: message,
        duration: 2,
      })
    },
    requestFailed(err) {
      this.$notification['error']({
        message: '错误',
        description: ((err.response || {}).data || {}).message || '请求出现错误，请稍后再试',
        duration: 4,
      })
      this.registerBtn = false
    },
  },
  watch: {
    'state.passwordLevel'(val) {
      console.log(val)
    },
  },
}
</script>
<style lang="less">
.user-register {
  &.error {
    color: #ff0000;
  }

  &.warning {
    color: #ff7e05;
  }

  &.success {
    color: #52c41a;
  }
}

.user-layout-register {
  .ant-input-group-addon:first-child {
    background-color: #fff;
  }
}
</style>
<style lang="less" scoped>
.user-layout-register {
  & > h3 {
    font-size: 16px;
    margin-bottom: 20px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .register-button {
    width: 50%;
  }

  .login {
    float: right;
    line-height: 40px;
  }
}
</style>